package com.ma.service.impl;

import cn.hutool.json.JSONUtil;
import com.ma.common.dto.SysMenuDto;
import com.ma.common.dto.SysMenuVoDto;
import com.ma.entity.SysMenu;
import com.ma.entity.SysUser;
import com.ma.mapper.SysMenuMapper;
import com.ma.mapper.SysMenuVoMapper;
import com.ma.mapper.SysUserMapper;
import com.ma.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
@Slf4j
public class SysMenuServiceImpl  implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysMenuVoMapper sysMenuVoMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysMenu> selectPermsById(Long id) {
        return sysMenuMapper.selectPermsById(id);
    }

    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        //通过上下文获取username
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserMapper.getAllByUsername(username);
        List<SysMenuVoDto> sysMenuList = sysMenuVoMapper.getNavByUserId(sysUser.getId());


        // 转树状结构
        List<SysMenuVoDto> menuTree = buildTreeMenu(sysMenuList);
        // 实体转DTO
        return convert(menuTree);

    }

    @Override
    public SysMenuVoDto selectALLById(int id) {
        return sysMenuVoMapper.selectALLById(id) ;
    }

    @Override
    public List<SysMenuVoDto> getMenuTree() {
        //获取所有菜单信息
        List<SysMenuVoDto> sysMenuVoDtos = sysMenuVoMapper.selectOrderAll();
        //转成树状结构
        return buildTreeMenu(sysMenuVoDtos);

    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);
    }

    @Override
    public void update(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }

    @Override
    public int countByParentId(Long id) {
        return sysMenuMapper.countByParentId(id);
    }

    @Override
    public void deleteBy(Long id) {
         sysMenuMapper.deleteBy(id);
    }

    @Override
    public SysMenu selectParentIdById(Long menuId) {
       return sysMenuMapper.selectParentIdById(menuId);
    }

    @Override
    public int getAllByparentId(Long menuId) {
        return sysMenuMapper.getAllByparentId(menuId);
    }

    private List<SysMenuDto> convert(List<SysMenuVoDto> menuTree) {
        List<SysMenuDto> menuDtos = new ArrayList<>();

        menuTree.forEach(m -> {
            SysMenuDto dto = new SysMenuDto();

            dto.setId(m.getId());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setComponent(m.getComponent());
            dto.setPath(m.getPath());

            if (m.getChildren().size() > 0) {

                // 子节点调用当前方法进行再次转换
                dto.setChildren(convert(m.getChildren()));
            }

            menuDtos.add(dto);
        });

        return menuDtos;
    }

    private List<SysMenuVoDto> buildTreeMenu(List<SysMenuVoDto> menus) {

        List<SysMenuVoDto> finalMenus = new ArrayList<>();
        //先寻找各自的子节点
        //先循环出所有数据
        // 先各自寻找到各自的孩子

        for (SysMenuVoDto menu : menus) {

            //在循环出当前数据的子节点
            for (SysMenuVoDto e : menus) {

                if (menu.getId() == e.getParentId()) {
                    menu.getChildren().add(e);

                }
            }

            // 提取出父节点
            if (menu.getParentId() == 0L) {

                finalMenus.add(menu);
            }
        }
        return finalMenus;
    }
}




